﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using LitJson;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace NeteasyMusic
{
    
    
    public class NEMusicInfo
    {
        public string name { get; set; }
        public string picurl { get; set; }
        public string id { get; set; }
        public  string url { get; set; }
        public string alias { get; set; }
        
    }
    
    public class Music
    {
        
        public static List<NEMusicInfo> GetSearch(string content,string limit, SearchType type)
        {
                List<NEMusicInfo> nEs = new List<NEMusicInfo>();
            string data=string.Format("s={0}&limit={1}&type={2}&offset=10", content, limit, type);
            var json =request.Post("http://music.163.com/api/search/pc",data);
            try
            {

            var jsonData = JsonMapper.ToObject(json);
                foreach (JsonData item in jsonData["result"]["songs"])
            {
                NEMusicInfo minfo = new NEMusicInfo()
                {
                    id = item["id"].ToString(),
                    name = item["name"].ToString(),
                    url = string.Format("http://music.163.com/song/media/outer/url?id=[{0}].mp3", item["id"].ToString()),
                    picurl = string.Empty

                 };
                nEs.Add(minfo);
            }
            return nEs;
            }catch(Exception exp)
            {
                Debug.WriteLine(request.Post("http://music.163.com/api/search/pc","s=befall&type=1&limit=4"));
                Debug.WriteLine(json);
                throw exp;
            }
            
            

        }
    }
}
